package com.itheima.stock.controller;

import com.itheima.stock.pojo.domain.EditUserDomain;
import com.itheima.stock.pojo.domain.SysUserDomain;
import com.itheima.stock.pojo.domain.UserByIdDomain;
import com.itheima.stock.pojo.domain.UserDomain;
import com.itheima.stock.pojo.entity.SysUser;
import com.itheima.stock.pojo.vo.req.*;
import com.itheima.stock.pojo.vo.resp.LoginRespVo;
import com.itheima.stock.pojo.vo.resp.R;
import com.itheima.stock.service.UserService;
import com.jayway.jsonpath.internal.function.ParamType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api")
@Api(value = "用户认证相关接口定义",tags = "用户功能-用户登录功能")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "根据用户名查询用户信息", notes = "查询用户信息", response = SysUser.class)
    @ApiImplicitParam(
            paramType = "path", name = "userName", value = "用户名",dataType = "string", required = true
    )
    @GetMapping("/user/{userName}")
    public SysUser getUserByUserName(@PathVariable("userName") String userName){

        return userService.findUserInfoByUserName(userName);
    }

    @ApiOperation(value = "用户登录功能",notes = "用户登录",response = R.class)
    @PostMapping("/login")
    public R<UserDomain> login(@RequestBody LoginReqVo vo){
        return null;
    }
    @ApiOperation(value = "生成验证码功能",response = R.class)
    @GetMapping("/captcha")
    public R<HashMap<String,String>>  getCaptchaCode(){
        return userService.getCaptchaCode();
    }

    @PreAuthorize("hasAuthority('sys:user:list')")
    @PostMapping("/users")
    public R<PageResult<SysUserDomain>> UserPage(@RequestBody UserPage userPage){
        return userService.UserPage(userPage);
    }
    @PostMapping("/user")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public R saveUser(@RequestBody UserReqVo userReqVo){
        return userService.saveUser(userReqVo);
    }
    @GetMapping("/user/roles/{userId}")
    public R<Map<String,Object>> getRoleInfo(@PathVariable String userId){
        return userService.getRoleInfo(userId);
    }
    @PutMapping("/user/roles")
    @PreAuthorize("hasAuthority('sys:user:role:update')")
    public R editRole(@RequestBody EditRole editRole){
        return userService.editRole(editRole);
    }
    @DeleteMapping("/user")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public R delUser(@RequestBody List<Long> userIds){
        return userService.delUser(userIds);
    }
    @GetMapping("/user/info/{userId}")
    public R<UserByIdDomain> getUserById(@PathVariable String userId){
        return userService.getUserById(userId);
    }
    @PutMapping("/user")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public R editUserById(@RequestBody EditUserDomain editUserDomain){
        return userService.editUserById(editUserDomain);
    }

}
